#!/usr/bin/perl -w

#=======================================================================
# compr-test
# File ID: 8cb08b6c-fa50-11dd-aa63-000475e441b9
# Display a graph which compares the compress ratio from several 
# programs. Quick & Dirty™.
#
# Character set: UTF-8
# ©opyleft 2006– Øyvind A. Holm <sunny@sunbase.org>
# License: GNU General Public License version 2 or later, see end of 
# file for legal stuff.
#=======================================================================

use strict;
use Getopt::Long;
use Time::HiRes qw{ gettimeofday };

$| = 1;

our $Debug = 0;

our %Opt = (

    'debug' => 0,
    'full-scale' => 0,
    'help' => 0,
    'time' => 0,
    'verbose' => 0,
    'version' => 0,

);

our $progname = $0;
$progname =~ s/^.*\/(.*?)$/$1/;
our $VERSION = "0.00";

Getopt::Long::Configure("bundling");
GetOptions(

    "debug" => \$Opt{'debug'},
    "full-scale|f" => \$Opt{'full-scale'},
    "help|h" => \$Opt{'help'},
    "time|t" => \$Opt{'time'},
    "verbose|v+" => \$Opt{'verbose'},
    "version" => \$Opt{'version'},

) || die("$progname: Option error. Use -h for help.\n");

$Opt{'debug'} && ($Debug = 1);
$Opt{'help'} && usage(0);
if ($Opt{'version'}) {
    print_version();
    exit(0);
}

LOOP:
for my $File (@ARGV) {
    my $orig_size = file_size($File);
    my $yrange = $Opt{'full-scale'} ? "[0:100]" : "[:]";

    unless (length($orig_size)) {
        warn("$progname: $File: Unable to stat() file\n");
        next LOOP;
    }
    if ($orig_size == 0) {
        warn("$progname: $File: File cannot be empty\n");
        next LOOP;
    }
    unless (open(DatFP, ">", "$File.compr")) {
        warn("$progname: $File.compr: Cannot open file for write: $!\n");
        next LOOP;
    }

    if ($Opt{'time'}) {
        system("cat $File >/dev/null");
    }
    for my $Comp (1..9) {
        my $tmp_prefix = "$File.$Comp.tmp";

        $Opt{'verbose'} && print("gzip -$Comp\n");
        my $gzip_start = gettimeofday;
        system("gzip -$Comp <$File >$tmp_prefix.gz");
        my $gzip_end = gettimeofday;
        my $gzip_time = $gzip_end-$gzip_start;
        my $compr_gzip = file_size("$tmp_prefix.gz");
        unlink("$tmp_prefix.gz");

        $Opt{'verbose'} && print("bzip2 -$Comp\n");
        my $bzip_start = gettimeofday;
        system("bzip2 -$Comp <$File >$tmp_prefix.bz2");
        my $bzip_end = gettimeofday;
        my $bzip_time = $bzip_end-$bzip_start;
        my $compr_bzip = file_size("$tmp_prefix.bz2");
        unlink("$tmp_prefix.bz2");

        $Opt{'verbose'} && print("zip -$Comp\n");
        my $zip_start = gettimeofday;
        system("zip -$Comp - - 2>/dev/null <$File >$tmp_prefix.zip");
        my $zip_end = gettimeofday;
        my $zip_time = $zip_end-$zip_start;
        my $compr_zip = file_size("$tmp_prefix.zip");
        unlink("$tmp_prefix.zip");

        $Opt{'verbose'} && print("lzip -$Comp\n");
        my $lzip_start = gettimeofday;
        system("lzip -$Comp <$File >$tmp_prefix.lz");
        my $lzip_end = gettimeofday;
        my $lzip_time = $lzip_end-$lzip_start;
        my $compr_lzip = file_size("$tmp_prefix.lz");
        unlink("$tmp_prefix.lz");

        $Opt{'verbose'} && print("lrzip -L $Comp\n");
        my $lrzip_start = gettimeofday;
        system("lrzip -L $Comp <$File >$tmp_prefix.lrz");
        my $lrzip_end = gettimeofday;
        my $lrzip_time = $lrzip_end-$lrzip_start;
        my $compr_lrzip = file_size("$tmp_prefix.lrz");
        unlink("$tmp_prefix.lrz");

        $Opt{'verbose'} && print("lrzip -z -L $Comp\n");
        my $lrzip_z_start = gettimeofday;
        system("lrzip -z -L $Comp <$File >$tmp_prefix.lrz");
        my $lrzip_z_end = gettimeofday;
        my $lrzip_z_time = $lrzip_z_end-$lrzip_z_start;
        my $compr_lrzip_z = file_size("$tmp_prefix.lrz");
        unlink("$tmp_prefix.lrz");

        $Opt{'verbose'} && print("xz -$Comp\n");
        my $xz_start = gettimeofday;
        system("xz -$Comp <$File >$tmp_prefix.xz");
        my $xz_end = gettimeofday;
        my $xz_time = $xz_end-$xz_start;
        my $compr_xz = file_size("$tmp_prefix.xz");
        unlink("$tmp_prefix.xz");

        $Opt{'verbose'} && print("xz -e -$Comp\n");
        my $xz_e_start = gettimeofday;
        system("xz -e -$Comp <$File >$tmp_prefix.xz");
        my $xz_e_end = gettimeofday;
        my $xz_e_time = $xz_e_end-$xz_e_start;
        my $compr_xz_e = file_size("$tmp_prefix.xz");
        unlink("$tmp_prefix.xz");

        my ($ratio_gzip, $ratio_bzip, $ratio_zip, $ratio_lzip, $ratio_lrzip, $ratio_lrzip_z, $ratio_xz, $ratio_xz_e);

        if ($Opt{'time'}) {
            # $ratio_gzip = $gzip_time / ($compr_gzip/$orig_size);
            # $ratio_bzip =  $bzip_time / ($compr_bzip/$orig_size);
            # $ratio_zip  =  $zip_time / ($compr_zip/$orig_size);
            $ratio_gzip = $gzip_time;
            $ratio_bzip = $bzip_time;
            $ratio_zip = $zip_time;
            $ratio_lzip = $lzip_time;
            $ratio_lrzip = $lrzip_time;
            $ratio_lrzip_z = $lrzip_z_time;
            $ratio_xz = $xz_time;
            $ratio_xz_e = $xz_e_time;
        } else {
            $ratio_gzip = ($compr_gzip/$orig_size) * 100;
            $ratio_bzip = ($compr_bzip/$orig_size) * 100;
            $ratio_zip = ($compr_zip/$orig_size) * 100;
            $ratio_lzip = ($compr_lzip/$orig_size) * 100;
            $ratio_lrzip = ($compr_lrzip/$orig_size) * 100;
            $ratio_lrzip_z = ($compr_lrzip_z/$orig_size) * 100;
            $ratio_xz = ($compr_xz/$orig_size) * 100;
            $ratio_xz_e = ($compr_xz_e/$orig_size) * 100;
        }

        print(DatFP "$Comp\t$ratio_gzip\t$ratio_bzip\t$ratio_zip\t$ratio_lzip\t$ratio_lrzip\t$ratio_lrzip_z\t$ratio_xz\t$ratio_xz_e\n");
    }
    close(DatFP);
    my $gnuplot_file = "$File.gnuplot";
    if (open(GnuplFP, ">", "$gnuplot_file")) {
        print(GnuplFP <<END);
#!/usr/bin/gnuplot -persist

set yrange $yrange
plot "$File.compr" using 1:2 title "gzip" w lp pt 3, \\
"$File.compr" using 1:3 title "bzip2" w lp pt 3, \\
"$File.compr" using 1:4 title "zip" w lp pt 3, \\
"$File.compr" using 1:5 title "lzip" w lp pt 3, \\
"$File.compr" using 1:6 title "lrzip" w lp pt 3, \\
"$File.compr" using 1:7 title "lrzip -z" w lp pt 3, \\
"$File.compr" using 1:8 title "xz" w lp pt 3, \\
"$File.compr" using 1:9 title "xz -e" w lp pt 3
END
        close(GnuplFP);
    }
    system("gnuplot -persist $gnuplot_file");
}

sub file_size {
    my $File = shift;
    my ($dev, $ino, $mode, $nlink, $uid, $gid, $rdev, $size, $atime, 
        $mtime, $ctime, $blksize, $blocks) = stat($File);
    return(defined($size) ? $size : "");
}

sub print_version {
    # Print program version {{{
    print("$progname v$VERSION\n");
    # }}}
} # print_version()

sub usage {
    # Send the help message to stdout {{{
    my $Retval = shift;

    if ($Opt{'verbose'}) {
        print("\n");
        print_version();
    }
    print(<<END);

Usage: $progname [options] [file [files [...]]]

Display a graph which compares the compress ratio from several programs. 
Quick & Dirty™. Needs gnuplot(1).

Options:

  -f, --full-scale
    Use Y range 0–100.
  -h, --help
    Show this help.
  -t, --time
    Show number of microseconds used instead of size.
  -v, --verbose
    Increase level of verbosity. Can be repeated.
  --version
    Print version information.
  --debug
    Print debugging messages.

END
    exit($Retval);
    # }}}
} # usage()

sub msg {
    # Print a status message to stderr based on verbosity level {{{
    my ($verbose_level, $Txt) = @_;

    if ($Opt{'verbose'} >= $verbose_level) {
        print(STDERR "$progname: $Txt\n");
    }
    # }}}
} # msg()

sub D {
    # Print a debugging message {{{
    $Debug || return;
    my @call_info = caller;
    chomp(my $Txt = shift);
    my $File = $call_info[1];
    $File =~ s#\\#/#g;
    $File =~ s#^.*/(.*?)$#$1#;
    print(STDERR "$File:$call_info[2] $$ $Txt\n");
    return("");
    # }}}
} # D()

__END__

# Plain Old Documentation (POD) {{{

=pod

=head1 NAME



=head1 SYNOPSIS

 [options] [file [files [...]]]

=head1 DESCRIPTION



=head1 OPTIONS

=over 4

=item B<-h>, B<--help>

Print a brief help summary.

=item B<-v>, B<--verbose>

Increase level of verbosity. Can be repeated.

=item B<--version>

Print version information.

=item B<--debug>

Print debugging messages.

=back

=head1 BUGS



=head1 AUTHOR

Made by Øyvind A. Holm S<E<lt>sunny@sunbase.orgE<gt>>.

=head1 COPYRIGHT

Copyleft © Øyvind A. Holm E<lt>sunny@sunbase.orgE<gt>
This is free software; see the file F<COPYING> for legalese stuff.

=head1 LICENCE

This program is free software: you can redistribute it and/or modify it 
under the terms of the GNU General Public License as published by the 
Free Software Foundation, either version 2 of the License, or (at your 
option) any later version.

This program is distributed in the hope that it will be useful, but 
WITHOUT ANY WARRANTY; without even the implied warranty of 
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along 
with this program.
If not, see L<http://www.gnu.org/licenses/>.

=head1 SEE ALSO

=cut

# }}}

# vim: set fenc=UTF-8 ft=perl fdm=marker ts=4 sw=4 sts=4 et fo+=w :
